import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtCharts

Rectangle {
    Layout.fillWidth: true
    Layout.fillHeight: true

    ChartView {
        id: chartView
        title: "饼图示例"
        titleFont.bold: true
        titleFont.pointSize: 14

        anchors.fill: parent
        antialiasing: true

        PieSeries {
            id: pieSeries

            // 中心圆圈大小：最小 0 <---> 1 填满饼图
            //holeSize: 0.5

            // 饼图相对大小：最小 0 <---> 1 填满chartview
            size: 0.7

            // 在chartview中的相对位置：左侧 0 <---> 1 右侧
            horizontalPosition: 0.5

            // 在chartview中的相对位置：顶部 0 <---> 1 底部
            verticalPosition: 0.5

            PieSlice {
                id: slice1

                label: "六月"
                labelVisible: true
                color: "#4CAF50"
                value: 13.5
                exploded: true
            }

            PieSlice {
                label: "七月"
                labelVisible: true
                color: "#1296FF"
                value: 10.9
            }
            PieSlice {
                label: "八月";
                labelVisible: true
                color: "#FFC107"
                value: 8.6
            }
            PieSlice {
                label: "九月";
                labelVisible: true
                color: "#9C27B0"
                value: 8.2
            }
            PieSlice {
                label: "十月"
                labelVisible: true
                color: "#FF9800"
                value: 6.8
            }

            // 点击交互 (slice1已设置为弹出状态)
            property var lastSlice: slice1

            onClicked: function(slice) {
                for(var i = 0; i < pieSeries.count; i++) {
                    var curSlice = pieSeries.at(i);

                    // 设置当前点击的扇形弹出
                    if (curSlice === slice) {

                        // 如果点击的还是上一个扇形
                        if (pieSeries.lastSlice && pieSeries.lastSlice === curSlice) {
                            curSlice.exploded = !curSlice.exploded;
                        } else {
                            curSlice.exploded = true;
                        }

                        // 记下当前点击的扇形
                        pieSeries.lastSlice = curSlice;
                        continue;
                    }

                    curSlice.exploded = false;
                }
            }
        }
    }
}
